package com.sky.controller.admin;


import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@Slf4j
@Api("菜品管理")
public class DishController {

@Autowired
private DishService dishService;
@Autowired
private RedisTemplate redisTemplate;


    private void cleanCache(String patten) {
        Set<String> keys = redisTemplate.keys(patten);
        redisTemplate.delete(keys);
    }





    /**
     * 新增菜品
     * @param dishDTO
     * @return
     */
    @ApiOperation("新增菜品")
    @PostMapping("/admin/dish")
    public Result SaveDish(@RequestBody DishDTO dishDTO) {

        dishService.SaveDish(dishDTO);

        // 精确删除缓存
        String key = "dish:" + dishDTO.getCategoryId();
        redisTemplate.delete(key);

        return Result.success();
    }


    /**
     * 分页查询菜品信息
     * @param dishPageQueryDTO
     * @return
     */
    @GetMapping("/admin/dish/page")
    @ApiOperation("分页查询菜品信息")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO) {
        log.info("分页查询菜品信息:{}", dishPageQueryDTO);
        PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);
        return Result.success(pageResult);
    }


    /**
     * 删除菜品
     * @param ids
     * @return
     */
    @DeleteMapping("/admin/dish")
    @ApiOperation("删除菜品")
    public Result deleteDish(@RequestParam List<Long> ids) {
        log.info("删除菜品信息：{}",ids);
        dishService.deleteByIds(ids);


        // 清理所有缓存
        String patten = "dish_";
        cleanCache(patten);
        return Result.success();
    }


    /**
     * 根据id查询菜品信息
     * @param id
     * @return
     */
    @GetMapping("/admin/dish/{id}")
    @ApiOperation("根据id查询菜品信息")
    public Result<DishVO> getDishById(@PathVariable Long id) {

        log.info("根据id查询菜品:{}",id);
        DishVO dishVO = dishService.getDishById(id);
        return Result.success(dishVO);
    }


    /**
     * 修改菜品信息
     * @param dishDTO
     * @return
     */
    @PutMapping("/admin/dish")
    @ApiOperation("修改菜品")
    public Result update(@RequestBody DishDTO dishDTO) {
        log.info("修改菜品:{}",dishDTO.getName());
        dishService.update(dishDTO);

        // 清理所有缓存
        String patten = "dish_*";
        cleanCache(patten);

        return Result.success("修改成功！");
    }


    /**
     * 修改菜品状态
     * @param id
     * @param status
     * @return
     */
    @PostMapping("/admin/dish/status/{status}")
    @ApiOperation("修改菜品状态")
    public Result setStatus(Long id, @PathVariable Integer status) {
        log.info("修改菜品状态:{}",id);
        dishService.setStatus(id,status);

        // 清理所有缓存
        String patten = "dish_*";
        cleanCache(patten);

        return Result.success("状态修改成功！");
    }


    @GetMapping("/admin/dish/list")
    @ApiOperation("根据套餐分类id查询菜品")
    public Result<List<Dish>> getDishByCategoryId(Long categoryId) {
        log.info("根据套餐分类id查询菜品:{}",categoryId);
        List<Dish> dishList = dishService.dishList(categoryId);

        return Result.success(dishList);
    }

}
